package com.guli.gulimall.product.service.impl;

import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.dao.CategoryDao;
import com.guli.gulimall.product.entity.AttrEntity;
import com.guli.gulimall.product.entity.CategoryEntity;
import com.guli.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.product.dao.AttrGroupDao;
import com.guli.gulimall.product.entity.AttrGroupEntity;
import com.guli.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryDao categoryDao;


    @Autowired
    AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params,Long catelogId) {
        QueryWrapper<AttrGroupEntity> w = new QueryWrapper<>();
        if (!catelogId.equals(0l)){
            w.eq("catelog_id",catelogId);
        }

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
               w
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupVo> attrGroupList(Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id",catelogId);
        List<AttrGroupEntity> attrGroupEntityList = baseMapper.selectList(wrapper);

        List<AttrGroupVo> collect = attrGroupEntityList.stream().map(item -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);

            QueryWrapper<AttrEntity> w = new QueryWrapper<>();

            w.eq("attr_type", 1);
            w.eq("attr_group_id", item.getAttrGroupId());
            List<AttrEntity> attrs = attrDao.selectList(w);
            attrGroupVo.setAttrs(attrs);
            return attrGroupVo;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public AttrGroupEntity getById(Serializable id) {


        AttrGroupEntity byId = super.getById(id);

        Long catelogId = byId.getCatelogId();
        List<Long> longs = new ArrayList<>();
        getLogPath(longs,catelogId);

        Collections.reverse(longs);
        Long[] ids = longs.toArray(new Long[longs.size()]);

        byId.setCatelogPath(ids);

        return byId;
    }
    private void getLogPath(List<Long> list,Long catelogId){
        list.add(catelogId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        if(!categoryEntity.getParentCid().equals(0l))
            getLogPath(list,categoryEntity.getParentCid());
    }
}